<?php

@session_start();

require 'conf.php';

function error_msg($msg) 
{
    echo json_encode(array('error' => $msg));
}

function deleteuser($sessiontoken, $uid)
{ 
    global $MW_host;
    $fields = array('ADMIN_DELETE_USER' => '',
                    'SESSION' => $sessiontoken,
                    'UID' => $uid);

    $fields = http_build_query($fields);

    $h = curl_init($MW_host); 
    curl_setopt($h, CURLOPT_POST, 1);
    curl_setopt($h, CURLOPT_POSTFIELDS, $fields); 
    curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($h, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($fields))); 

    $res = curl_exec($h);

    if(!mb_check_encoding($res, 'UTF-8'))
        $res = utf8_encode($res);
    $data = json_decode($res);
    return $data;
}

if ($_SESSION['username'] == 'admin') {
    if (isset($_POST['uid']) && is_numeric($_POST['uid'])) {
        $res = deleteuser($_SESSION['sessiontoken'], $_POST['uid']);
        if ($res === null) {
            error_msg("Ett fel uppstod i komunikationen med mellan-servern");
        } else {
            echo json_encode($res);
        }
    } else {
        error_msg("Ogiltig uid");
    }
} else {
    error_msg("Ogiltig session. Logga in igen.");
}

?>
